#include <OneWire.h>
#include <DallasTemperature.h>
// Data draad in pin 2 op de Arduino
#define ONE_WIRE_BUS 6 // Moet officieel 2 zijn=conflict met relay
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire); 
int inByte = 0;         // incoming serial byte
int test = 0;

// Declaratie van de relais
byte relayPin[4] = {2,3,4,5};
//D2 -> RELAY1
//D3 -> RELAY2
//D4 -> RELAY3
//D5 -> RELAY4

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Aansluiting van relais op output zetten
  for(int i = 0; i < 4; i++)  pinMode(relayPin[i],OUTPUT);
  // Start up the library
  sensors.begin();
}

void loop(void)
{ 
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
   
    inByte = Serial.read();
    // Serial.println(inByte); Geeft letter weer dat als commando is gegeven.

    if (inByte == 84) {  // Als T dan temperatuur meten en terug sturen.
          sensors.requestTemperatures(); // Send the command to get temperatures
            Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire               
              Serial.print(sensors.getTempCByIndex(1)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire               
                Serial.print(sensors.getTempCByIndex(2)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire               
                  Serial.print(sensors.getTempCByIndex(3)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire               
    }
// Aanzetten van de Relais
    if (inByte == 65) digitalWrite(relayPin[0],HIGH); // Relais 1 aan zetten
    if (inByte == 66) digitalWrite(relayPin[1],HIGH); // Relais 2 aan zetten
    if (inByte == 67) digitalWrite(relayPin[2],HIGH); // Relais 3 aan zetten
    if (inByte == 68) digitalWrite(relayPin[3],HIGH); // Relais 4 aan zetten
    
// Uitzetten van de Relais
    if (inByte == 69) digitalWrite(relayPin[0],LOW); // Relais 1 uit zetten
    if (inByte == 70) digitalWrite(relayPin[1],LOW); // Relais 2 uit zetten
    if (inByte == 71) digitalWrite(relayPin[2],LOW); // Relais 3 uit zetten
    if (inByte == 72) digitalWrite(relayPin[3],LOW); // Relais 4 uit zetten
  }
}

